﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Entities;
namespace WebApplication
{
    public partial class HienThiGioHang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        void BindData()
        {
            gvGioHang.DataSource = GioHang.TheHien.Ds_ChiTiet;
            gvGioHang.DataBind();
        }
        protected void gvGioHang_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[3].Text = "Tổng tiền: " + GioHang.TheHien.TongTien() + " VND";
            }
        }

        protected void gvGioHang_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "XoaCT_GH")
            {
                int _idSanPham = Convert.ToInt32(e.CommandArgument);
                GioHang.TheHien.XoaCT_GH(_idSanPham);
            }
            BindData();
        }

        protected void btnTiepTuc_Click(object sender, EventArgs e)
        {
            Response.Redirect("DMSanPham.aspx");
        }

        protected void btnCap_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvGioHang.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    try
                    {
                        int productId = Convert.ToInt32(gvGioHang.DataKeys[row.RowIndex].Value);
                        int quantity = int.Parse(((TextBox)row.Cells[2].FindControl("txtSoLuong")).Text);
                       GioHang.TheHien.CaiDatSoLuong(productId, quantity);
                    }
                    catch (FormatException) { }
                }
            }
            BindData();
        }

        protected void btnXoaGioHang_Click(object sender, EventArgs e)
        {
            GioHang.TheHien.Ds_ChiTiet = new List<ChiTietGioHang>();
            Session["ASPGioHang"] = null;
            Response.Redirect("~/HienThiGioHang.aspx");
        }

    }
}